import StorageUtil from "./Storage.ts";
import JPush from "jpush-react-native";

class NotificationClass {

	/**
	 * 收到服务器通知
	 */
	public static async serverNotification(): Promise<void> {
		const badge = await StorageUtil.load<number>('badge')
		if (badge) {
			await StorageUtil.save('badge', badge + 1)
			JPush.setBadge({ badge: badge + 1, appBadge: badge + 1 });
		} else {
			await StorageUtil.save('badge', 1)
			JPush.setBadge({ badge: 1, appBadge: 1 });
		}
	}

	/**
	 * 打开通知通知
	 */
	public static async notificationOpened(): Promise<void> {
		const badge = await StorageUtil.load<number>('badge')
		if (badge) {
			await StorageUtil.save('badge', badge - 1)
			JPush.setBadge({ badge: badge - 1, appBadge: badge - 1 });
		} else {
			await StorageUtil.save('badge', 0)
			JPush.setBadge({ badge: 0, appBadge: 0 });
		}
	}

	/**
	 * 新增本地通知
	 * @param params
	 */
	public static async addedLocalNotifications(params: AddNotify): Promise<void> {
		JPush.addLocalNotification(params)
		const badge = await StorageUtil.load<number>('badge')
		if (badge) {
			await StorageUtil.save('badge', badge + 1)
			JPush.setBadge({ badge: badge + 1, appBadge: badge + 1 });
		} else {
			JPush.setBadge({ badge: 1, appBadge: 1 });
			await StorageUtil.save('badge', 1)
		}
	}
}

export default NotificationClass;
